import { Component, OnInit } from '@angular/core';
import { CommonModule } from '@angular/common';
import { EndUserDto } from 'src/app/models/sys/end-user.dto';
import { ApiService } from 'src/app/services/api.service';
import { EndUserQueryParam } from 'src/app/models/param/end-user-query.param';
import { Consts } from 'src/app/consts/consts';
import { NzTableQueryParams } from 'ng-zorro-antd/table';
import { FormBuilder, FormControl, UntypedFormGroup } from '@angular/forms';

@Component({
  selector: 'app-end-user',
  templateUrl: './end-user.component.html',
  styleUrl: './end-user.component.less'
})
export class EndUserComponent implements OnInit {
  searchForm!: UntypedFormGroup;

  // Table
  endUsers: EndUserDto[] = [];
  pageIndex = 1;
  pageSize = 12;
  total = 0;

  constructor(private _api: ApiService, private _fb: FormBuilder) {

  }
  ngOnInit(): void {
    this.createSearchForm();
    this.onSearch();
  }

  createSearchForm() {
    this.searchForm = this._fb.group({
      username: new FormControl(''),
      phoneNumber: new FormControl(''),
      uid: new FormControl('')
    });
  }

  onSearch() {
    const param = new EndUserQueryParam();
    param.pageNum = 1;
    param.pageSize = 10;

    this._api.getEndUserList(param).subscribe((resp) => {
      if (resp.code == Consts.RESP_OK && resp.data) {
        this.endUsers = resp.data;
      }
    });
  }

  onReset() {}
  
  onPageParamChange(params: NzTableQueryParams) {
    this.pageIndex = params.pageIndex;
    this.pageSize = params.pageSize;
    this.onSearch();
  }

}
